import {
  queryLeaderServingList,
  addManage,
  updateManage,
  deleteManage,
} from '@/services/LeaderServing';

export default {
  namespace: 'LeaderServing',

  state: {
    leaderServingData: [],
    updateDidFinish: false,
  },

  effects: {
    *queryLeaderServingList({ payload }, { call, put }) {
      try {
        const response = yield call(queryLeaderServingList, payload);
        yield put({
          type: 'setPayload',
          payload: {
            leaderServingData: response.data,
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *addManageData({ payload }, { call, put }) {
      try {
        const response = yield call(addManage, payload);
        yield put({
          type: 'setPayload',
          payload: {
            updateDidFinish: new Date().getTime(),
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *updateManageData({ payload }, { call, put }) {
      try {
        const response = yield call(updateManage, payload);
        yield put({
          type: 'setPayload',
          payload: {
            updateDidFinish: new Date().getTime(),
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
    *deleteManageData({ payload }, { call, put }) {
      try {
        const response = yield call(deleteManage, payload);
        yield put({
          type: 'setPayload',
          payload: {
            updateDidFinish: new Date().getTime(),
          },
        });
      } catch (e) {
        console.log('Error:', e);
      }
    },
  },

  reducers: {
    setPayload(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
